package com.intct.util;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author gufg
 * @since 2025-07-29 14:21
 */
public class RowkeyUtil {
    /**
     * HBase Rowkey生成规则
     * @return
     */
    public static String getRowkey(String rowkey, int numPartitions) {
        int rowkeyCode = (rowkey.hashCode() & Integer.MAX_VALUE) % numPartitions;

        System.out.println("rowkeyCode" + rowkeyCode);

        // 左补全的方式--补3个0   0000~0008
        String prefix = StringUtils.leftPad(String.valueOf(rowkeyCode), 4, "0");

        System.out.println("prefix = " + prefix);

        // rowkey进竺md5加密
        String md5 = DigestUtils.md5Hex(rowkey);
        // 截取字符串前12值
        String suffix = md5.substring(0, 12);
        return prefix + suffix;
    }
}
